﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace HZY.Admin.Services
{
    using System.Threading.Tasks;
    using HZY.EFCore.Repository;
    using HZY.Models;
    using HZY.Models.Sys;
    using HZY.Toolkits;
    using HZY.EFCore.Base;
    using HZY.Admin.Services.Core;
    using HZY.EFCore;

    public class DeviceTypeService : ServiceBase<DeviceType>
    {
        public DeviceTypeService(EFCoreContext _db)
            : base(_db)
        {

        }

        #region CURD 基础

        /// <summary>
        /// 获取数据源
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="Rows"></param>
        /// <param name="Search"></param>
        /// <returns></returns>
        public async Task<TableViewModel> FindListAsync(int Page, int Rows, DeviceType Search)
        {
            var query = this.Query()
                .WhereIF(w => w.DeviceType_Name.Contains(Search.DeviceType_Name), !string.IsNullOrWhiteSpace(Search?.DeviceType_Name))
                .OrderBy(w => w.DeviceType_Num)
                .Select(w => new
                {
                    w.DeviceType_Num,
                    w.DeviceType_Name,
                    w.DeviceType_CreateTime,
                    _ukid = w.DeviceType_ID
                })
                ;

            return await this.db.AsTableViewModelAsync(query, Page, Rows, typeof(DeviceType));
        }

        /// <summary>
        /// 新增\修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Guid> SaveAsync(DeviceType model)
        {
            await this.InsertOrUpdateAsync(model);

            return model.DeviceType_ID;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(List<Guid> Ids)
            => await this.DeleteAsync(w => Ids.Contains(w.DeviceType_ID));

        /// <summary>
        /// 加载表单 数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<Dictionary<string, object>> LoadFormAsync(Guid Id)
        {
            var res = new Dictionary<string, object>();

            var Model = await this.FindByIdAsync(Id);

            res[nameof(Id)] = Id;
            res[nameof(Model)] = Model.NullSafe();

            return res;
        }


        #endregion

    }
}
